package com.taotao.rest.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.service.RedisService;

@Service
public class RedisServiceImpl implements RedisService{

	@Autowired
	private JedisClient jedisClient;
	
	@Value("${TB_CONTENT_KEY}")
	private String TB_CONTENT_KEY;
	@Value("${TB_ITEM_CAT_PARENT_KEY}")
	private String TB_ITEM_CAT_PARENT_KEY;
	@Value("REDIS_ITEM_KEY")
	private String REDIS_ITEM_KEY;
	
	//内容缓存同步
	@Override
	public TaotaoResult synContent(Long cid) {
		if(cid==null)
			return TaotaoResult.build(400, "错误的参数");
		
		try {
			jedisClient.hdel(TB_CONTENT_KEY, cid+"");
		}
		catch (Exception e) {
			return TaotaoResult.build(500, "删除缓存失败");
		}
		
		return TaotaoResult.ok();
	}

	//商品类目缓存
	@Override
	public TaotaoResult synItemCat(Long pid) {
		if(pid==null)
			return TaotaoResult.build(400, "错误的参数");
		String key=pid+"";

		try {
			jedisClient.hdel(TB_ITEM_CAT_PARENT_KEY, key);
		}
		catch (Exception e) {
			return TaotaoResult.build(500, "删除缓存失败");
		}
		
		return TaotaoResult.ok();
	}

	//Item缓存同步
	@Override
	public TaotaoResult synItem(Long itemId) {
		if(itemId==null)
			return TaotaoResult.build(400, "错误的参数");
		String infoKey=itemId+":info";
		String descKey=itemId+":desc";
		String paramKey=itemId+":param";
		try {
			jedisClient.hdel(REDIS_ITEM_KEY, infoKey);
			jedisClient.hdel(REDIS_ITEM_KEY, descKey);
			jedisClient.hdel(REDIS_ITEM_KEY, paramKey);
		}
		catch (Exception e) {
			return TaotaoResult.build(500, "删除缓存失败");
		}
		
		return TaotaoResult.ok();
	}
	
	@Override
	public TaotaoResult synItemInfo(Long itemId) {
		if(itemId==null)
			return TaotaoResult.build(400, "错误的参数");
		String infoKey=itemId+":info";
		
		try {
			jedisClient.hdel(REDIS_ITEM_KEY, infoKey);
		}
		catch (Exception e) {
			return TaotaoResult.build(500, "删除缓存失败");
		}
		
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult synitemIDesc(Long itemId) {
		if(itemId==null)
			return TaotaoResult.build(400, "错误的参数");
		String descKey=itemId+":desc";
		
		try {
			jedisClient.hdel(REDIS_ITEM_KEY, descKey);
		}
		catch (Exception e) {
			return TaotaoResult.build(500, "删除缓存失败");
		}
		
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult synItemParam(Long itemId) {
		if(itemId==null)
			return TaotaoResult.build(400, "错误的参数");
		String paramKey=itemId+":param";
		
		try {
			jedisClient.hdel(REDIS_ITEM_KEY, paramKey);
		}
		catch (Exception e) {
			return TaotaoResult.build(500, "删除缓存失败");
		}
		
		return TaotaoResult.ok();
	}



}
